package language.advanced.Concurrent;

import lombok.SneakyThrows;
import org.apache.poi.ss.formula.functions.T;
import org.junit.Test;

import java.util.Random;

public class ThreadTest {
    public static void main(String[] args) {

        //Runable是个函数式接口
        new Thread(() -> System.out.println("aaa")).start();

        //内部类Proposer的匿名内部类
        new Thread(){
            @Override
            public void run(){
                System.out.println("aaa");
            }
        }.start();
    }

    @Test
    public void a() throws InterruptedException {
        class A implements Runnable {
            private int a=10;

            @Override
            public void run() {
                try {
                    System.out.println("线程执行: "+ Thread.currentThread().getName());
                    a = a+1;
                    Thread.sleep(2000);
                    System.out.println("finish sleep");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            public void k() {
                System.out.println("sleep can speak");
                System.out.println(a);
                System.out.println("主线程执行: "+ Thread.currentThread().getName());

            }
        }
        A a = new A();
        new Thread(a).start();
        Thread.sleep(1000);
        a.k();
        while (true);
    }

    static class B extends Thread{
        @Override
        public void run() {
            System.out.println("thread start");
        }
    }

    static class C implements Runnable{
        @Override
        public void run() {
            System.out.println("thread start");
        }
    }

    @Test
    public void threadStart(){
        new B().start();
        new Thread(new C()).start(); //Runnable要启动，必须先生成一个thread对象
    }
}
